package cn.tiger;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.Lifecycle;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class SpringBeanLifeCycleWithEvents implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean, Lifecycle {

    private String beanName;
    private BeanFactory beanFactory;
    private ApplicationContext applicationContext;

    // BeanNameAware 接口方法，在 Bean 名称被设置时调用
    @Override
    public void setBeanName(String name) {
        this.beanName = name;
        System.out.println("BeanNameAware: Bean name is " + name);
    }

    // BeanFactoryAware 接口方法，在 Bean 工厂被设置时调用
    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        this.beanFactory = beanFactory;
        System.out.println("BeanFactoryAware: Bean factory is set");
    }

    // ApplicationContextAware 接口方法，在应用上下文被设置时调用
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        System.out.println("ApplicationContextAware: Application context is set");
    }

    // InitializingBean 接口方法，在 Bean 属性被设置后调用
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean: Bean properties are set");
    }

    // 自定义初始化方法，用于自定义初始化逻辑
    public void customInitMethod() {
        System.out.println("Custom init method: Custom initialization logic");
    }

    // DisposableBean 接口方法，在 Bean 被销毁时调用
    @Override
    public void destroy() throws Exception {
        System.out.println("DisposableBean: Bean is being destroyed");
    }

    // 自定义销毁方法，用于自定义清理逻辑
    public void customDestroyMethod() {
        System.out.println("Custom destroy method: Custom cleanup logic");
    }

    // Lifecycle 接口方法，在 Bean 启动时调用
    @Override
    public void start() {
        System.out.println("Lifecycle: Bean is started");
    }

    // Lifecycle 接口方法，在 Bean 停止时调用
    @Override
    public void stop() {
        System.out.println("Lifecycle: Bean is stopped");
    }

    // Lifecycle 接口方法，检查 Bean 是否正在运行
    @Override
    public boolean isRunning() {
        return false; // For simplicity, we always return false here
    }

    // Spring 事件监听器，在 Spring 上下文刷新时调用
    @EventListener
    public void handleContextRefreshedEvent(ContextRefreshedEvent event) {
        System.out.println("ContextRefreshedEvent: Context is refreshed");
    }

    // Spring 事件监听器，在 Spring 上下文启动时调用
    @EventListener
    public void handleContextStartedEvent(ContextStartedEvent event) {
        System.out.println("ContextStartedEvent: Context is started");
    }

    // Spring 事件监听器，在 Spring 上下文停止时调用
    @EventListener
    public void handleContextStoppedEvent(ContextStoppedEvent event) {
        System.out.println("ContextStoppedEvent: Context is stopped");
    }

    // Spring 事件监听器，在 Spring 上下文关闭时调用
    @EventListener
    public void handleContextClosedEvent(ContextClosedEvent event) {
        System.out.println("ContextClosedEvent: Context is closed");
    }

    // 自定义方法，用于演示生命周期
    public void customMethod() {
        System.out.println("Custom method: This method is called during the lifecycle");
    }
}
